<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.library.entity.Book" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.library.entity.Admin" %>
<!DOCTYPE html>
<html>
<head>
    <title>图书管理系统 - <%= request.getAttribute("isEdit") != null ? "编辑" : "添加" %>书籍</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .header {
            background-color: #f8f9fa;
            padding: 10px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            margin: 0;
            font-size: 20px;
            color: #333;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .logout-btn {
            margin-left: 15px;
            padding: 5px 10px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            color: #333;
        }

        .nav-container {
            width: 200px;
            background-color: #f8f9fa;
            float: left;
            min-height: calc(100vh - 50px);
            border-right: 1px solid #e0e0e0;
        }

        .admin-label {
            padding: 15px;
            background-color: #e9ecef;
            font-weight: bold;
            border-bottom: 1px solid #ddd;
        }

        .nav-container ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .nav-container li {
            padding: 15px;
            cursor: pointer;
            border-bottom: 1px solid #e0e0e0;
        }

        .nav-container li:hover {
            background-color: #e9ecef;
        }

        .nav-container li a {
            text-decoration: none;
            color: #333;
            display: block;
        }

        .nav-container li.active {
            background-color: #007bff;
        }

        .nav-container li.active a {
            color: white;
        }

        .content-container {
            margin-left: 200px;
            padding: 20px;
        }

        .form-card {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            max-width: 600px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: #333;
        }

        .form-control {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }

        .form-control:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 0 2px rgba(0,123,255,.25);
        }

        textarea.form-control {
            height: 80px;
            resize: vertical;
        }

        .btn {
            padding: 8px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            text-decoration: none;
            display: inline-block;
        }

        .btn:hover {
            background-color: #0069d9;
        }

        .btn-success {
            background-color: #28a745;
        }

        .btn-success:hover {
            background-color: #218838;
        }

        .btn-secondary {
            background-color: #6c757d;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
        }

        .required {
            color: red;
        }

        .alert {
            padding: 10px 15px;
            margin-bottom: 15px;
            border: 1px solid transparent;
            border-radius: 4px;
        }

        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }

        .form-actions {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="header">
        <h1>图书管理系统</h1>
        <div class="user-info">
            欢迎您，<%=((Admin) request.getSession().getAttribute("admin")).getUsername()%>
            <a href="../common?method=logout" class="logout-btn">退出登录</a>
        </div>
    </div>

    <!-- 左侧导航栏 -->
    <div class="nav-container">
        <div class="admin-label">管理端</div>
        <ul>
            <li><a href="../admin?method=adminIndex">首页</a></li>
            <li class="active"><a href="../admin/book?method=bookManage">书籍</a></li>
            <li><a href="../admin/bookcopy?method=bookCopyManage">书籍副本</a></li>
            <li><a href="../admin/payrec?method=fineRecord">学生罚款记录</a></li>
        </ul>
    </div>

    <!-- 中间内容区域 -->
    <div class="content-container">
        <h2><%= request.getAttribute("isEdit") != null ? "编辑" : "添加" %>书籍</h2>
        
        <!-- 显示消息 -->
        <% 
        String successMessage = (String) session.getAttribute("successMessage");
        if (successMessage != null) {
            session.removeAttribute("successMessage");
        %>
        <div class="alert alert-success"><%= successMessage %></div>
        <% } %>
        
        <% 
        String errorMessage = (String) session.getAttribute("errorMessage");
        if (errorMessage != null) {
            session.removeAttribute("errorMessage");
        %>
        <div class="alert alert-danger"><%= errorMessage %></div>
        <% } %>
        
        <div class="form-card">
            <% 
            Book book = (Book) request.getAttribute("book");
            boolean isEdit = request.getAttribute("isEdit") != null;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            %>
            
            <form action="${pageContext.request.contextPath}/admin/bookManage" method="post">
                <input type="hidden" name="method" value="<%= isEdit ? "updateBook" : "addBook" %>">
                <% if (isEdit && book != null) { %>
                <input type="hidden" name="id" value="<%= book.getId() %>">
                <% } %>
                
                <div class="form-group">
                    <label for="ISBN">ISBN <span class="required">*</span></label>
                    <% if (isEdit) { %>
                    <input type="text" class="form-control" id="ISBN" name="ISBN" 
                           value="<%= book != null && book.getISBN() != null ? book.getISBN() : "" %>" readonly style="background-color: #f5f5f5;">
                    <small class="form-text text-muted">编辑时不允许修改ISBN</small>
                    <% } else { %>
                    <input type="text" class="form-control" id="ISBN" name="ISBN" 
                           value="<%= book != null && book.getISBN() != null ? book.getISBN() : "" %>" required>
                    <% } %>
                </div>
                
                <div class="form-group">
                    <label for="bname">书名 <span class="required">*</span></label>
                    <input type="text" class="form-control" id="bname" name="bname" 
                           value="<%= book != null && book.getBname() != null ? book.getBname() : "" %>" required>
                </div>
                
                <div class="form-group">
                    <label for="author">作者 <span class="required">*</span></label>
                    <input type="text" class="form-control" id="author" name="author" 
                           value="<%= book != null && book.getAuthor() != null ? book.getAuthor() : "" %>" required>
                </div>
                
                <div class="form-group">
                    <label for="publisher">出版社 <span class="required">*</span></label>
                    <input type="text" class="form-control" id="publisher" name="publisher" 
                           value="<%= book != null && book.getPublisher() != null ? book.getPublisher() : "" %>" required>
                </div>
                
                <div class="form-group">
                    <label for="pubDate">出版日期</label>
                    <input type="date" class="form-control" id="pubDate" name="pubDate" 
                           value="<%= book != null && book.getPubDate() != null ? dateFormat.format(book.getPubDate()) : "" %>">
                </div>
                
                <div class="form-group">
                    <label for="clcNum">中图分类号</label>
                    <input type="text" class="form-control" id="clcNum" name="clcNum" 
                           value="<%= book != null && book.getClcNum() != null ? book.getClcNum() : "" %>">
                </div>
                
                <div class="form-group">
                    <label for="introduction">简介</label>
                    <textarea class="form-control" id="introduction" name="introduction" 
                              placeholder="请输入书籍简介..."><%= book != null && book.getIntroduction() != null ? book.getIntroduction() : "" %></textarea>
                </div>
                
                <div class="form-actions">
                    <button type="submit" class="btn btn-success"><%= isEdit ? "更新" : "添加" %>书籍</button>
                    <a href="${pageContext.request.contextPath}/admin/book?method=bookManage" class="btn btn-secondary">取消</a>
                </div>
            </form>
        </div>
    </div>
</body>
</html> 